package com.ymatou.smsproxy.api.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.smsproxy.api.parameter.SendMessageBean;
import com.ymatou.smsproxy.api.parameter.SmsCallBackBean;
import com.ymatou.smsproxy.api.service.SendMessageCall;
import com.ymatou.smsproxy.api.service.SmsCallBackCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SendSmsCallService;
import com.ymttest.database.mongo.ApplicationConfigBean;
import com.ymttest.database.mongo.ApplicationConfigWapper;
import com.ymttest.database.mongo.ChannelConfigBean;
import com.ymttest.database.mongo.ChannelConfigWapper;
import com.ymttest.database.mongo.PhoneBlackListWapper;
import com.ymttest.database.mongo.SmsRecordBean;
import com.ymttest.database.mongo.SmsRecordWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 发送短信接口 
 * 
 * @author chenjiazhu
 */
@TestSuite
public class Ts_SmsCallBack {

	private static SendMessageBean smb;
	private static SendMessageCall smc;
	
	private static SmsCallBackBean scbb;
	private static SmsCallBackCall scbc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送短信接口");
	}

	@Before
	public void caseUp() {
		smb = new SendMessageBean();
		smc = new SendMessageCall("GET","PARAM");
		scbb=new SmsCallBackBean();
		scbc=new SmsCallBackCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(smc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendMessage_001() {
		Logger.start(true,"发送短信成功");
		try {
			
			String appid="Test";
			String phone=SendSmsCallService.TESTPHONE;
			String content="autotest_"+TestDataManager.getRandomNum(10);
			
			smb.setAppId(appid);
			smb.setPhone(phone);
			smb.setContent(content);

			smc.setData(smb);
			smc.callService();

			Logger.comment("根据appid、content、phone到数据库表SmsRecord中查询相应记录来验证是否存入SmsRecord表");
			
			List<String> phones=new ArrayList<String>();
			phones.add(phone);
			
			SmsRecordBean smsRecord = new SmsRecordWapper().getCollectionByAppIdContentPhones(appid, content, phones);
			Logger.debug(FormatUtil.beanToGSONString(smsRecord));
			
			scbb.setContent(content);
			scbb.setPhone(phone);
			scbb.setId(smsRecord.get_id());
			scbb.setAppId(appid);
			scbb.setApiName("SendMessage");
			scbb.setClientIp(TestDataManager.getRandomIp());
			scbb.setServerIp(TestDataManager.getRandomIp());
			scbc.setData(scbb);
			scbc.callService();
			//状态为1的时候 fail , 发送失败的时候fail
			//状态为6 7 ok
			Logger.verifyEquals("\"fail\"", scbc.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	private void setDefaultSendMessageBean()
	{
		String appid="Test";
		String phone="13000000000";
		String content="autotest_发送短信_"+TestDataManager.getRandomNum(10);
		
		smb.setAppId(appid);
		smb.setPhone(phone);
		smb.setContent(content);
	}
	
	private void verifyReturn(boolean isInBlackList) throws Exception
	{
		Thread.sleep(1000);
		Date now = new Date();
		
		String appid=smb.getAppId();
		String content=smb.getContent();
		String phone=smb.getPhone();
		
		Logger.comment("根据appid、content、phone到数据库表SmsRecord中查询相应记录来验证是否存入SmsRecord表");
		
		List<String> phones=new ArrayList<String>();
		phones.add(phone);
		
		SmsRecordBean smsRecord = new SmsRecordWapper().getCollectionByAppIdContentPhones(appid, content, phones);

		Logger.verifyNotNull(smsRecord, "验证数据存入SmsRecord成功");
		
		ApplicationConfigBean applicationConfig = new ApplicationConfigWapper().getCollectionByIdAndAppId("Test-SendMessage",appid);
		ChannelConfigBean channelConfig = new ChannelConfigWapper().getCollectionById(applicationConfig.getChannelId());
		
		Logger.verifyEquals("SendMessage", smsRecord.getApiName(), "验证数据库表SmsRecord中getApiName字段为SendSmsVerifyCode");
		
		if(isInBlackList)
		{
			Logger.verifyIsNull(smsRecord.getChannelId(), "验证中风控时数据库表SmsRecord中ChannelId字段为空");
			Logger.verifyIsNull(smsRecord.getChannelName(), "验证中风控时数据库表SmsRecord中ChannelName字段为空");
		}
		else {
			Logger.verifyEquals(applicationConfig.getChannelId(), smsRecord.getChannelId(), "验证数据库表SmsRecord中ChannelId字段与表ApplicationConfig中ChannelId一致");
			Logger.verifyEquals(channelConfig.getName(), smsRecord.getChannelName(), "验证数据库表SmsRecord中ChannelName字段与表ChannelConfig中Name一致");
		}
		
		Logger.verifyEquals(TestDataManager.getLocalIp(), smsRecord.getClientIp(), "验证数据库表SmsRecord中ClientIp字段与发送端Ip一致");
		String host=EnvSetup.getSystemUrl("new_api_smsproxy").replace("http://", "").replace("/", "");
		Logger.verifyEquals(TestDataManager.getServerIP(host), smsRecord.getServerIp(), "验证数据库表SmsRecord中ServerIp字段与服务端Ip一致");

		Date createdTime=YMTDateUtil.getDateFromMongo(smsRecord.getCreatedTime().toString());
		Date sendedTime=YMTDateUtil.getDateFromMongo(smsRecord.getSendedTime().toString());
	
		Logger.verifyEquals(true, Math.abs(createdTime.getTime()-now.getTime())/(1000*60)<20, "验证数据库表SmsRecord中createdTime字段正确，值为："+smsRecord.getCreatedTime().toString()+",当前时间为"+now.toLocaleString());
		
//		if(isInBlackList)
//		{
//			Logger.verifyEquals("1900-1-1 0:00:00", sendedTime.toLocaleString(), "验证数据库表SmsRecord中createdTime字段正确，值为："+smsRecord.getSendedTime().toString()+",当前时间为"+now.toLocaleString());
//		}
//		else {
			Logger.verifyEquals(true, Math.abs(sendedTime.getTime()-now.getTime())/(1000*60)<20, "验证数据库表SmsRecord中sendedTime字段正确，值为："+smsRecord.getSendedTime().toString()+",当前时间为"+now.toLocaleString());
//		}
		
		if(isInBlackList)
		{
			Logger.verifyEquals(8, smsRecord.getMessageAction(), "验证数据库表SmsRecord中MessageAction字段值为8:手机号在黑名单中InBlackList");
		}
		else {
			Logger.verifyEquals(7, smsRecord.getMessageAction(), "验证数据库表SmsRecord中MessageAction字段值为7,非生产环境下，正常情况下是 发送消息到服务商失败SendMessageFail = 7");
		}
		
	}
}